//
//  RCMerchantCommentCell.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-9-6.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCMerchantCommentCell.h"
#import "LVTTTAttributedLabel.h"
#import "NSString+NSString_Ex_.h"
#import "CommonDefines.h"
#import "NSAttributedString+LVAddition.h"
#import "UIColor-Expanded.h"
#import "RCMerchantCommentListDataModel.h"

#define kGetMoreNormalImageFile              @"image_merchant_arrow_down.png"
#define kGetMoreSelectedImageFile            @"image_merchant_arrow_upper.png"
#define kStartActiveImageFile    @"image_start_active.png"
#define kStartInActiveImageFile  @"image_merchant_inactive.png"



#define kLineLeading                         9
#define kLineDefaultHeight                   15
#define kLine1LabelFrame                     CGRectMake(17, 50, 286, 12*2+9)
#define kBackgroundImgVFrame                 CGRectMake(10, 10, 300,100)
#define kExpandBtnFrame                      CGRectMake(280, self.frame.size.height - 40, 20, 27)

#define kCellHighLightedTextColor            0xFF4694C8
#define kNormalFontColor                     0xFF505050
#define kLastLineIndent                      16

//#define kLineImageFile           @"image_line_type3.png"
#define kLineImageFile           @"image_order_line.png"

@interface RCMerchantCommentCell()

@property(nonatomic,retain)UILabel *userNameLabel;
@property(nonatomic,retain)UIImageView *startImageView1;
@property(nonatomic,retain)UIImageView *startImageView2;
@property(nonatomic,retain)UIImageView *startImageView3;
@property(nonatomic,retain)UIImageView *startImageView4;
@property(nonatomic,retain)UIImageView *startImageView5;
@property(nonatomic,retain)UILabel *priceLabel;
@property(nonatomic,retain)LVTTTAttributedLabel *contentLabel;
@property(nonatomic,retain)UILabel *timeLabel;
@property(nonatomic,assign)BOOL    hasMore;
@property(nonatomic,retain)UIImageView *lineImageView;

@end

@implementation RCMerchantCommentCell
@synthesize userNameLabel = _userNameLabel;
@synthesize startImageView1 = _startImageView1;
@synthesize startImageView2 = _startImageView2;
@synthesize startImageView3 = _startImageView3;
@synthesize startImageView4 = _startImageView4;
@synthesize startImageView5 = _startImageView5;
@synthesize priceLabel = _priceLabel;
@synthesize contentLabel = _contentLabel;
@synthesize timeLabel = _timeLabel;
@synthesize delegate = _delegate;
@synthesize isExpand = _isExpand;
@synthesize getMoreBtn = _getMoreBtn;
@synthesize hasMore = _hasMore;
@synthesize lineImageView = _lineImageView;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self createPlaceHolderView];
    }
    return self;
}

-(void)dealloc{
    
    self.userNameLabel = nil;
    self.startImageView1 = nil;
    self.startImageView2 = nil;
    self.startImageView3 = nil;
    self.startImageView4 = nil;
    self.startImageView5 = nil;
    self.priceLabel = nil;
    self.contentLabel = nil;
    self.timeLabel = nil;
    self.delegate = nil;
    self.getMoreBtn = nil;
    self.lineImageView = nil;
    [super dealloc];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
    if (selected)
    {
        [self getMoreClicked:self.getMoreBtn];
    }
}

+ (float)getCellHightWithContent:(NSString *)contentStr withIsExpand:(BOOL)isExpand
{
    BOOL hasContent = ![NSString stringIsNullOrEmpty:contentStr];
    float height = 0.0f;
    CGSize contentSize = CGSizeZero;
    
    if (!hasContent)
    {
        return 0.0f;
    }
    
    LVTTTAttributedLabel* labDetail4Content = [[LVTTTAttributedLabel alloc] initWithFrame:CGRectZero];
    labDetail4Content.numberOfLines = 0;
    labDetail4Content.font = [UIFont fontWithName:kFontNameLightHeiti
                                             size:12];
    labDetail4Content.leading = kLineLeading;
    labDetail4Content.verticalAlignment = TTTAttributedLabelVerticalAlignmentTop;
    labDetail4Content.text = contentStr;
    
    contentSize = [labDetail4Content.attributedText sizeWithConstrainedToSize:CGSizeMake(286, MAXFLOAT)];
    
    [labDetail4Content release];
    labDetail4Content = nil;
    
    
    CGSize originalContentSize = contentSize;
    
    if (CGSizeEqualToSize(CGSizeZero, originalContentSize))
    {
        return 0.0;
    }
    else
    {
        
        if (!isExpand)
        {
            if (contentSize.height > kLineDefaultHeight * 2 + kLineLeading)
            {
                contentSize.height = kLineDefaultHeight * 2 + kLineLeading;
            }
            
        }
        
        
        height += kLine1LabelFrame.origin.y;
        height += contentSize.height;
        height +=34; //下面的高度
    }
    
    return height;
}

-(void)createPlaceHolderView{
    
    _userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(17, 9, 286, 17)];
    _userNameLabel.backgroundColor = [UIColor clearColor];
    _userNameLabel.textColor = [UIColor colorWithHex:0xff505050];
    _userNameLabel.font = [UIFont fontWithName:kFontNameHelvetica size:16];
    [self addSubview:_userNameLabel];
    
    _startImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(17, 30, 14, 14)];
    _startImageView1.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView1];
    
    _startImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(32, 30, 14, 14)];
    _startImageView2.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView2];
    
    _startImageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(47, 30, 14, 14)];
    _startImageView3.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView3];
    
    _startImageView4 = [[UIImageView alloc]initWithFrame:CGRectMake(62, 30, 14, 14)];
    _startImageView4.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView4];
    
    _startImageView5 = [[UIImageView alloc]initWithFrame:CGRectMake(77, 30, 14, 14)];
    _startImageView5.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView5];
    
    _priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 31, 200, 13)];
    _priceLabel.backgroundColor = [UIColor clearColor];
    _priceLabel.textColor = [UIColor colorWithHex:0xff989797];
    _priceLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    [self addSubview:_priceLabel];
    
    //ticket text label
    _contentLabel = [[LVTTTAttributedLabel alloc] initWithFrame:CGRectZero];
    _contentLabel.numberOfLines = 0;
    _contentLabel.font = [UIFont fontWithName:kFontNameLightHeiti
                                         size:12];
    _contentLabel.textColor = [UIColor colorWithHex:kNormalFontColor];
    _contentLabel.backgroundColor = [UIColor clearColor];
    _contentLabel.leading = kLineLeading;
    _contentLabel.verticalAlignment = TTTAttributedLabelVerticalAlignmentTop;
    _contentLabel.lastLineIndent = 0;
    
    [self addSubview:_contentLabel];
    
    
    if (!self.getMoreBtn)
    {
        _getMoreBtn = [[UIButton alloc] initWithFrame:CGRectZero];
        [_getMoreBtn setBackgroundColor:[UIColor clearColor]];
        [_getMoreBtn setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kGetMoreNormalImageFile
                                                                                               ofType:nil]]
                     forState:UIControlStateNormal];
        [_getMoreBtn addTarget:self
                        action:@selector(getMoreClicked:)
              forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_getMoreBtn];
        
    }
    
    _timeLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    _timeLabel.backgroundColor = [UIColor clearColor];
    _timeLabel.textColor = [UIColor colorWithHex:0xff989797];
    _timeLabel.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    [self addSubview:_timeLabel];
    
    _lineImageView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _lineImageView.backgroundColor = [UIColor clearColor];
    UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kLineImageFile ofType:nil]];
    [_lineImageView setImage:image];
    [self addSubview:_lineImageView];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (void)getMoreClicked:(id)sender
{
    if (!_hasMore)
    {
        return;
    }
    
    
    [self.getMoreBtn setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.isExpand ? kGetMoreSelectedImageFile : kGetMoreNormalImageFile
                                                                                               ofType:nil]]
                     forState:UIControlStateNormal];
    if (_delegate && [_delegate respondsToSelector:@selector(getMoreButtonClicked:)]) {
        [_delegate getMoreButtonClicked:self.getMoreBtn.tag];
    }
    
    
}

- (void)bindDataWithContentStr:(RCMerchantCommentListItem *)itemDataModel withIsExpand:(BOOL)isExpand
{
    if (nil == itemDataModel) {
        return;
    }
    self.isExpand = isExpand;
    
    _userNameLabel.hidden = YES;
    if (![NSString stringIsNullOrEmpty:itemDataModel.userName]) {
        _userNameLabel.hidden = NO;
        _userNameLabel.text = itemDataModel.userName;
    }
    _startImageView1.hidden = YES;
    _startImageView2.hidden = YES;
    _startImageView3.hidden = YES;
    _startImageView4.hidden = YES;
    _startImageView5.hidden = YES;
    
    NSInteger score = itemDataModel.score;
    if (score > 0) {
        
        _startImageView1.hidden = NO;
        _startImageView2.hidden = NO;
        _startImageView3.hidden = NO;
        _startImageView4.hidden = NO;
        _startImageView5.hidden = NO;
        NSString *resourcePath = [[NSBundle mainBundle]resourcePath];
        UIImage *activeImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kStartActiveImageFile]];
        UIImage *inactiveImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kStartInActiveImageFile]];
        [_startImageView1 setImage:score > 0 ? activeImage : inactiveImage];
        [_startImageView2 setImage:score > 1 ? activeImage : inactiveImage];
        [_startImageView3 setImage:score > 2 ? activeImage : inactiveImage];
        [_startImageView4 setImage:score > 3 ? activeImage : inactiveImage];
        [_startImageView5 setImage:score > 4 ? activeImage : inactiveImage];
    }
    
    
    
    _contentLabel.text = itemDataModel.content;
    
    CGSize contentSize = [_contentLabel.attributedText sizeWithConstrainedToSize:CGSizeMake(kLine1LabelFrame.size.width, MAXFLOAT)];
    
    if (contentSize.height > kLineDefaultHeight * 2 +kLineLeading) {
        _hasMore = YES;
        if (isExpand) {
            self.contentLabel.lastLineIndent = 0;
            CGRect contentFrame = CGRectMake(kLine1LabelFrame.origin.x, kLine1LabelFrame.origin.y, kLine1LabelFrame.size.width, contentSize.height);
            self.contentLabel.frame = contentFrame;
        }else{
            self.contentLabel.lastLineIndent = 0;
            self.contentLabel.frame = kLine1LabelFrame;
        }
        
        CGRect frame = CGRectZero;
        
        frame = self.contentLabel.frame;
        
        
        frame.origin.y = frame.origin.y + frame.size.height - 2;
        frame.origin.x = 274;
        frame.size.width = 46;
        frame.size.height = 32;
        
        self.getMoreBtn.frame = frame;
        
    }else{
        _hasMore = NO;
        self.contentLabel.lastLineIndent = 0;
        CGRect contentFrame = CGRectMake(kLine1LabelFrame.origin.x, kLine1LabelFrame.origin.y, kLine1LabelFrame.size.width, contentSize.height);
        self.contentLabel.frame = contentFrame;
    }
    
    _timeLabel.hidden = YES;
    if (![NSString stringIsNullOrEmpty:itemDataModel.dateTime]) {
        _timeLabel.hidden = NO;
        _timeLabel.text = itemDataModel.dateTime;
        _timeLabel.frame = CGRectMake(17, CGRectGetMaxY(_contentLabel.frame)+10, 200, 13);
    }
    
    _lineImageView.frame = CGRectMake(0, CGRectGetMaxY(_contentLabel.frame)+33, 320, 1);
    self.getMoreBtn.hidden = !_hasMore;
}

@end
